library(tidyverse)
library(cowplot)
library(maps)
library(ggforce)
library(viridis)
studies <- read_csv("../data/studies_gsheet.csv")
sites <- read_csv("../data/sites_gsheet.csv")
df <- studies %>%
group_by(site) %>%
summarise(Nstudies = n_distinct(studyID),
Nspecies = n_distinct(species)) %>%
left_join(sites)
Joining, by = "site"
world <- map("world", plot = FALSE, fill = TRUE) %>% fortify()
# sanity check that coordinates are in the right country
# within a country, all Xs should be the same color
ggplot(df, aes(x = long, y = lat)) +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90") +
geom_point(aes(col = country), size = 5, shape = 4, stroke = 2, alpha = .8) +
coord_fixed(1.3, ylim = c(-55, 83.59961)) +
scale_color_viridis_d() +
theme_map() +
guides(col = "none")
rr ggsave(../graphs/map.pdf, width = 20, height = 10, scale = 2)
# japan
m1 <- m +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nstudies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(25, 45), xlim = c(125, 145)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
# europe
m2 <- m +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nstudies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(32, 60), xlim = c(-10, 30)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
# contiguous us
usa = map("state", plot = FALSE, fill = TRUE) %>% fortify() # "usa"
m3 <- m +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_polygon(data = usa, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nstudies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(25, 49), xlim = c(-127, -67)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
leg <- plot_grid(NA, legend, ncol = 1, rel_heights = c(.1, .9), labels = c("", "Number of Studies"), label_x = -.35)
Cannot convert object of class logical into a grob.
bottom_row <- plot_grid(m3, m2, NA, m1, NA, leg, nrow = 1, rel_widths = c(2, 1.18, .06, .85, .08, .7))
Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Cannot convert object of class logical into a grob.Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Cannot convert object of class logical into a grob.
plot_grid(mx, bottom_row, ncol = 1, rel_heights = c(2.4, 1)) +
theme(plot.margin = unit(c(.5, .5, 1.5, .5), "cm"))
Removed 3 rows containing missing values (geom_point).
ggsave("../graphs/map_insets.pdf", width = 8, height = 5.5, scale = 2)
ms <- ggplot(df, aes(x = long, y = lat)) +
geom_polygon(data = world, aes(group = group), fill = "grey90") +
geom_point(aes(size = Nstudies, fill = Nspecies), shape = 21, alpha = .8) +
theme_map() +
# # to make fill into discrete; overlap with size legend
# scale_size_area("Number of\nStudies", max_size = 15) +
# scale_fill_viridis_c("Number of\nStudies", guide = "legend") +
# to have them side by side
scale_fill_gradientn("", breaks = c(1, 11), colours = c("#598cde","#d4e324", "black")) +
scale_size_area("", max_size = 15, breaks = c(1, 20, 40, 60)) +
theme(legend.box = "horizontal") +
guides(fill = guide_colorbar(reverse = T, barheight = 9.5, order = 1),
size = guide_legend(keyheight = 2.6, title.theme = element_text(size = 4)))
legend <- get_legend(ms)
Removed 3 rows containing missing values (geom_point).
# plot_grid(legend)
msx <- ms +
annotate("rect", ymin = c(24, 31, 24), ymax = c(46, 62, 50), xmin = c(124, -12, -130),
xmax = c(146, 32, -64), size = .5, col = "grey", fill = NA) +
coord_fixed(1.3, ylim = c(-55, 83.59961)) +
theme(legend.position = "none")
# japan
ms1 <- ms +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nspecies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(25, 45), xlim = c(125, 145)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
# europe
ms2 <- ms +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nspecies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(32, 60), xlim = c(-10, 30)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
# contiguous us
usa = map("state", plot = FALSE, fill = TRUE) %>% fortify() # "usa"
ms3 <- ms +
geom_polygon(data = world, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_polygon(data = usa, aes(group = group), col = "black", fill = "grey90", lwd = .2) +
geom_point(aes(size = Nstudies, fill = Nspecies), shape = 21, alpha = .8) +
coord_fixed(1.3, ylim = c(25, 49), xlim = c(-127, -67)) +
theme(legend.position = "none") +
panel_border(color = "grey", size = .5)
legs <- plot_grid(NA, legend, ncol = 2, rel_heights = c(.1, .9), labels = c("Species", "Studies"), label_x = -.1)
Cannot convert object of class logical into a grob.
bottom_row <- plot_grid(ms3, ms2, NA, ms1, NA, legs, nrow = 1, rel_widths = c(2, 1.18, .06, .85, .08, .7))
Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Cannot convert object of class logical into a grob.Removed 3 rows containing missing values (geom_point).Removed 3 rows containing missing values (geom_point).Cannot convert object of class logical into a grob.
plot_grid(msx, bottom_row, ncol = 1, rel_heights = c(2.4, 1)) +
theme(plot.margin = unit(c(.5, .5, 1.5, .5), "cm"))
Removed 3 rows containing missing values (geom_point).
ggsave("../graphs/map_insets_col_species.pdf", width = 8, height = 5.5, scale = 2)
rr sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.5
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] viridis_0.5.1 viridisLite_0.3.0 ggforce_0.3.1 maps_3.3.0 cowplot_1.0.0
[6] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 readr_1.3.1
[11] tidyr_1.0.0 tibble_2.1.3 ggplot2_3.2.1 tidyverse_1.2.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 lubridate_1.7.4 lattice_0.20-38 prettyunits_1.0.2 ps_1.3.0
[6] assertthat_0.2.1 zeallot_0.1.0 digest_0.6.21 R6_2.4.0 cellranger_1.1.0
[11] backports_1.1.5 stats4_3.6.1 evaluate_0.14 httr_1.4.1 pillar_1.4.2
[16] rlang_0.4.0 lazyeval_0.2.2 readxl_1.3.1 rstudioapi_0.10 callr_3.3.2
[21] rmarkdown_1.16 labeling_0.3 loo_2.1.0 polyclip_1.10-0 munsell_0.5.0
[26] broom_0.5.2 compiler_3.6.1 modelr_0.1.5 xfun_0.10 rstan_2.19.2
[31] base64enc_0.1-3 pkgconfig_2.0.3 pkgbuild_1.0.5 htmltools_0.4.0 tidyselect_0.2.5
[36] gridExtra_2.3 matrixStats_0.55.0 crayon_1.3.4 withr_2.1.2 MASS_7.3-51.4
[41] grid_3.6.1 nlme_3.1-141 jsonlite_1.6 gtable_0.3.0 lifecycle_0.1.0
[46] magrittr_1.5 StanHeaders_2.19.0 scales_1.0.0 cli_1.1.0 stringi_1.4.3
[51] farver_1.1.0 xml2_1.2.2 generics_0.0.2 vctrs_0.2.0 tools_3.6.1
[56] glue_1.3.1 tweenr_1.0.1 hms_0.5.1 yaml_2.2.0 parallel_3.6.1
[61] processx_3.4.1 inline_0.3.15 colorspace_1.4-1 rvest_0.3.4 knitr_1.25
[66] haven_2.1.1